package edu.hku.c3330.hkucompanion.manager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Event 
{
	public static final short HIGHEST_PRIORITY = 10;
	public static final short LOWEST_PRIORITY = 0;
	
	long _id = -1;
	String title;
	String description;
	public String location;
	short priority;
	boolean isSingleEvent;
	boolean isCustomEvent = true;

	public void setTitle(String title) {
		this.title = title;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setPriority(short priority) {
		this.priority = priority;
	}

	SingleEventDateTime singleDateTime;
	List<RecurrenceDateTime> recurrences;

	public Event()
	{
		this("","","",(short)0);
		this._id = -1;
	}
	
	public boolean isCustomEvent()
	{
		return this.isCustomEvent;
	}
	
	public void cloneFrom(Event event)
	{
		this._id = event._id;
		this.title = event.title;
		this.description = event.description;
		this.location = event.location;
		this.priority = event.priority;
		this.isSingleEvent = event.isSingleEvent;
		this.singleDateTime = event.singleDateTime;
		this.recurrences = event.recurrences;
	}
	
	public Event(String title, String description)
	{
		this(title,description,"",(short)( ((HIGHEST_PRIORITY + LOWEST_PRIORITY) / 2)));
	}
	
	public Event(String title, String description, String location, short priority)
	{
		setEventContent(title, description, location, priority);
		singleDateTime = new SingleEventDateTime(null,null);
		recurrences = new ArrayList<RecurrenceDateTime>();
	}
	
	public void setDatabaseId(long id)
	{
		_id = id;
	}
	
	public long getDatabaseId()
	{
		return _id;
	}
	
	public boolean sentToDatabase()
	{
		return _id >= 0;
	}
	
	public void setEventContent(String title, String description, String location, short priority)
	{
		this.title = title;
		this.description = description;
		this.location = location;
		this.priority = priority;
	}
		
	public void setSingleEventDateTime(SingleEventDateTime dt)
	{
		this.singleDateTime = dt;
		recurrences.clear();
		isSingleEvent = true;
	}
		
	public void addRecurrence(RecurrenceDateTime rdt)
	{
		recurrences.add(rdt);
		if (singleDateTime.start == null || singleDateTime.start.compareTo(rdt.startDate) > 0)
		{
			singleDateTime.start = (Calendar)rdt.startDate.clone();
		}
		Calendar realEnd = (Calendar)rdt.endDate.clone();
		CalendarUtils.addCalendar(realEnd, rdt.eventDuration);
		
		if (singleDateTime.end == null || singleDateTime.end.compareTo(realEnd) > 0)
		{
			singleDateTime.end = realEnd;
		}
		isSingleEvent = false;
	}
	
	public void setRecurrenceList(List<RecurrenceDateTime> recurrences)
	{
		this.recurrences = recurrences;
	}
	
	public void setIsSingleEvent(boolean isSingleEvent)
	{
		this.isSingleEvent = isSingleEvent;
	}
	
	public List<SingleEventDateTime> getSingleDateTimesFromRecurrence()
	{
		if (isSingleEvent)
		{
			return null;
		}
		
		List<SingleEventDateTime> events = new ArrayList<SingleEventDateTime>();
		
		for (RecurrenceDateTime rdt : recurrences)
		{
			Calendar current = rdt.startDate;
			do
			{
				Calendar start = (Calendar)current.clone();
				Calendar end = (Calendar)start.clone();		
				CalendarUtils.addCalendar(end, rdt.eventDuration);
				SingleEventDateTime sedt = new SingleEventDateTime(start,end);
				events.add(sedt);
				CalendarUtils.addCalendar(current, rdt.recurrencePeriod);
			} while (current.compareTo(rdt.endDate) <= 0);
		}
		
		return events;
	}
	
	public String getTitle() {
		return title;
	}

	public String getDescription() {
		return description;
	}

	public String getLocation() {
		return location;
	}

	public short getPriority() {
		return priority;
	}

	public boolean isSingleEvent() {
		return isSingleEvent;
	}

	public SingleEventDateTime getSingleDateTime() {
		return singleDateTime;
	}

	public List<RecurrenceDateTime> getRecurrences() {
		return recurrences;
	}
		
	public class SingleEventDateTime
	{
		public Calendar start, end;
		public SingleEventDateTime(Calendar start, Calendar end)
		{
			this.start = start;
			this.end = end;
		}
	}
		
	public class RecurrenceDateTime
	{
		public Calendar startDate, endDate;
		public Duration eventDuration, recurrencePeriod;
		public RecurrenceDateTime (String course)
		{
			
		}
		public RecurrenceDateTime (Calendar startDate, Calendar endDate, Duration eventDuration ,Duration recurrencePeriod)
		{
			this.startDate = startDate;
			this.endDate = endDate;
			this.eventDuration = eventDuration;
			this.recurrencePeriod = recurrencePeriod;
		}
	}	

}
